package spotGUI.sideBar;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

import managers.DBManager;
import managers.EventsManager;
import utils.StackTracePrinter;
import elements.Event;


public class EventsPanel extends JPanel {
	
	
	private JLabel lblEvents;
	private JList list;
	private JScrollPane scrollPane;


	public EventsPanel() {
		
		setPreferredSize(new Dimension(270, 368));
		setMinimumSize(new Dimension(350, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{270, 0};
		gridBagLayout.rowHeights = new int[]{64, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		lblEvents = new JLabel("");
		lblEvents.setIcon(new ImageIcon(EventsPanel.class.getResource("/images/GUIBackgrounds/events.png")));
		lblEvents.setAlignmentX(Component.CENTER_ALIGNMENT);
		GridBagConstraints gbc_lblEvents = new GridBagConstraints();
		gbc_lblEvents.anchor = GridBagConstraints.SOUTH;
		gbc_lblEvents.insets = new Insets(0, 0, 5, 0);
		gbc_lblEvents.gridx = 0;
		gbc_lblEvents.gridy = 0;
		add(lblEvents, gbc_lblEvents);
		
		scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(scrollPane, gbc_scrollPane);
		
		list = new JList();
		list.setBackground(UIManager.getColor("Button.background"));
		list.setModel(new DefaultListModel());
		list.setCellRenderer(new EventsCellRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(list);
		
		try {
			updateEventList(DBManager.getConnection());
		} catch (SQLException e) {
			StackTracePrinter.print(e);
		}
		
	}
	
	
	public void updateEventList (Connection conn) throws SQLException {
		Calendar currentDate = Calendar.getInstance();
		String currentTime = new Timestamp(currentDate.getTimeInMillis()).toString();
		String nextMonth = getNextMonth(currentDate);
		String query = "select eventid, name, date, target from spotmessenger.event where date <= '"+nextMonth+
				"' and date >= '"+currentTime+"'";
		Statement stmt = conn.createStatement();
		stmt.executeQuery(query);
		ResultSet rs = stmt.executeQuery(query);
		while (rs.next()) {
			((DefaultListModel)list.getModel()).addElement(new Event(rs.getInt(1), rs.getString(2), rs.getTimestamp(3), rs.getString(4)));
		}
		if (list.getModel().getSize() == 0) {
			Event noEvents = new Event(EventsManager.NO_EVENTS, ResourceBundle.getBundle("spotGUI.sideBar.messages").getString("EventsPanel.noEvents"),
					new Timestamp(new Date().getTime()), null);
			noEvents.photo = new ImageIcon(EventsManager.class.getResource("/images/Icons/128x128/calendar_delete.png"));
			((DefaultListModel)list.getModel()).addElement(noEvents);
		}
		DBManager.closeResources(stmt, rs);
	}


	private String getNextMonth(Calendar cal) {
		cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);
		return new Timestamp(cal.getTimeInMillis()).toString();
	}
	
	
	private class EventsCellRenderer implements ListCellRenderer {

		private ImageIcon calendarIcon = new ImageIcon(EventsCellRenderer.class.getResource("/images/Icons/24x24/calendar_blank.png"));

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {

			Event event = (Event) value;

			EventReminderPanel listCellRendererComponent = new EventReminderPanel();
			listCellRendererComponent.setIcon(calendarIcon);
			
			String text = "";
			if (event.eventId != EventsManager.NO_EVENTS)
				text += DateFormat.getInstance().format(event.date)+"\n";
			text += event.name;
			listCellRendererComponent.setText(text);

			return listCellRendererComponent;
		}

	}

}
